Ext.define("Skood.view.user.UserGrid",{
	extend:"Ext.grid.Panel",
	
	alias :"widget.userGrid",
	title :'',
	store:'UserJsonStore',
	//添加到grid  
    selModel:Ext.create('Ext.selection.CheckboxModel'),  
    //表示可以选择行  
    disableSelection: false,  
    columnLines: true,
 	multiSelect: true,
 	height:500,
	initComponent:function(){
		var me = this;
		var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
		/*
		this.store={
				fields:['name','email'],
				data:[
				      {'name':'keege','email':'keege@skood.com'}
				      ]
		};
		*/
		/**
		this.columns=[
		              {header:'用户名',dataIndex:'name',flex:1},
		              {header:'邮箱' ,dataIndex:'email',flex:1}
		              ];
		 */
		var createAction = Ext.create('Ext.Action', {
	        //icon   : '../shared/icons/fam/delete.gif',  // Use a URL in the icon config
			iconCls:'user_add',
	        text: '新增',
	        scope:me,
	        handler: //onCreate(widget, event)
	        function(widget, event) {
	            var win = Ext.create('Skood.view.user.UserWindow');//.widget("userWindow");//不在views中加入‘user.UserWindow',就无法动态加载到UserWindow
				win.show();
	        }
	    });
	    var editAction = Ext.create('Ext.Action', {
	        iconCls: 'user_edit',
	        text: '修改',
	        disabled: true,
	        scope:me,
	        handler: function(widget, event) {
	            var record = me.getSelectionModel().getSelection()[0];
	            if (record) {
	                var win = Ext.widget("userWindow");//不在views中加入‘user.UserWindow',就无法动态加载到addEditUserWin
					//win.down('form').loadRecord(record);
					Ext.ComponentQuery.query('form > textfield[name=entityBean.name]')[0].setValue(record.get('name'));
					Ext.ComponentQuery.query('form > textfield[name=entityBean.nickName]')[0].setValue(record.get('nickName'));
					win.down('form').down('textfield[name=entityBean.email]').setValue(record.get('email'));
					win.down('form').down('textfield[name=entityBean.expireDate]').setValue(record.get('createdDate'));
					win.show();
	            }
	        }
	    });
		var removeAction = Ext.create('Ext.Action',{
			iconCls:'user_del',
            text:'删除',
            disabled:true,
            scope:me,
            handler:function(widget, event) {
	            var record = me.getSelectionModel().getSelection()[0];
	            if (record) {
					Ext.MessageBox.confirm('Confirm', 'Are you sure you want to do that?', function(btn){
					if (btn == 'yes'){
					        // process text value and close...
							var opts = {scope:me};
							Ext.Ajax.request({
								url:'/security/doRemoveUserAction.do?id='+record.get('id'),
								success:function(response,opts){
									var data = Ext.JSON.decode(response.responseText);
									if(data.success){
										Ext.Msg.alert('成功提示', '数据删除成功');
										me.store.loadPage(1);
									}
								},
								failure:function(response,opts){
									Ext.Msg.alert('提失败示', '数据删除失败');
								}
							}
							);
							
					    }
					});
	            }
            }
		});
		Ext.applyIf(me,{
			columns:[
	              {header:'用户名',dataIndex:'name'},
	              {header:'真实姓名',dataIndex:'nickName'},
	              {header:'邮箱' ,dataIndex:'email',width:120},
	              {header:'用户类型',dataIndex:'type',align:'center',renderer:this.typeFormat},
	              {header:'状态',dataIndex:'status',align:'center',renderer:this.statusFormat},
	              {header:'创建日期',dataIndex:'createdDate',renderer : Ext.util.Format.dateRenderer('Y-m-d')}
	              ],
	        tbar:[{  
                    xtype:'button',  
                    iconCls:'add',
                    text:'查询',
                    scope:me,
                    handler:this.onSearch 
                }/*,{  
                    xtype:'button',  
                    iconCls:'user_add',
                    text:'添加',
                    handler:this.onCreate
                },{  
                    xtype:'button', 
                    iconCls:'option',
                    text:'修改',
                    disabled:true,
                    handler:this.edit
                },{  
                    xtype:'button',  
                    iconCls:'remove',
                    text:'删除',
                    disabled:true,
                    hander:this.remove
                } */
                ,createAction,editAction,removeAction
            ],
	        dockedItems: [
	        //**
			{
			    xtype: 'pagingtoolbar',
			    dock: 'bottom',
			    store:'UserJsonStore',
			    displayInfo: true
			},//**/
			/*{
			    xtype: 'toolbar',
			    items: [{
			        text:'Add Something',
			        tooltip:'Add a new row',
			        iconCls:'add',
			        scope:me,
			        handler:me.refress
			    }, '-', {
			        text:'Options',
			        tooltip:'Set options',
			        iconCls:'option'
			    },'-',{
			        itemId: 'removeButton',
			        text:'Remove Something',
			        tooltip:'Remove the selected item',
			        iconCls:'remove',
			            disabled: true
			        }]
			     }*/
	          
	             //添加搜索控件  
	             {  
	                 dock: 'top',   
	                 xtype: 'toolbar',   
	                 items:{
				        xtype: 'form',
				        layout: {
				            type: 'table',
				            columns: 4
				        },
				        //id: 'searchUserForm',
				        url: '#',
				        //frame: true,
				        title: 'searchUserForm',
				        header: false,
				        bodyPadding: '5 5 0',
				        width: '100%',
				        fieldDefaults: {
				            msgTarget: 'side',
				            labelWidth: 75
				        },
				        plugins: {
				            ptype: 'datatip'
				        },
				        defaultType: 'textfield',
				        defaults: {width:170},
				        items: [{
				            fieldLabel: '用户名',
				            //afterLabelTextTpl: required,
				            name: 'name',
				            //id:'name',
				            allowBlank: true,
				            tooltip: '输入用户名查询',
				            labelWidth:60,
				            labelAlign:'right',
				            maxWidth :170
				        },{
				            fieldLabel: '真实姓名',
				            //afterLabelTextTpl: required,
				            name: 'nickName',
				            allowBlank: true,
				            labelWidth:60,
				            labelAlign:'right',
				            tooltip: '输入真实姓名查询'
				        }, {
				            fieldLabel: '邮箱',
				            //afterLabelTextTpl: required,
				            name: 'email',
				            //allowBlank: false,
				            labelWidth:60,
				            labelAlign:'right',
				            vtype:'email',
				            tooltip: '输入正确的邮箱地址'
				        }, {
				            fieldLabel: '创建时间',
				            name: 'createdDate',
				            xtype: 'datefield',
				            labelWidth:60,
				            labelAlign:'right',
				            tooltip: '输入创建时间'
				        }]}
	                 
	             }
                    
			],
	       loadMask: true 
		});
		
		me.callParent(arguments);
		
		this.store.loadPage(1);
		me.getSelectionModel().on({
        	selectionchange: function(sm, selections) {
	            if (selections.length) {
	                editAction.enable();
	                removeAction.enable();
	            } else {
	                editAction.disable();
	                removeAction.disable();
	            }
        	}
    	});
	},
	//自定义方法
	refress:function(){
		//this.store.loadPage(2);
		//this.store.load({params:xx});
		this.store.load();
	},
	onSearch:function(){
		//this.store.proxy.extraParams={'searchModel.user.name':Ext.getCmp('name').getValue()};//通过ID查找，但是加上ID如果在多个地方重复ID，显示会有问题
		//this.store.proxy.extraParams={'searchModel.user.name':Ext.ComponentQuery.query('form[title=searchUserForm] field:first')[0].getValue()};
		//this.store.proxy.extraParams={'searchModel.user.name':Ext.ComponentQuery.query('textfield[name=name]')[0].getValue()};
		this.store.proxy.extraParams={'searchModel.user.name':Ext.ComponentQuery.query('form[title=searchUserForm] > textfield[name=name]')[0].getValue()}
		this.store.loadPage(1);
		//this.store.load({params:[{name:Ext.getCmp('name').getValue()}]})
	},
	onCreate:function(widget, event){
		Ext.Msg.alert('Click', 'You did something.');
		var win = Ext.create('Skood.view.user.UserWindow');//.widget("userWindow");//不在views中加入‘user.UserWindow',就无法动态加载到UserWindow
		win.show();
		
	},
	
	statusFormat:function change(val) {
        if (val == 1) {
            return '<span style="color:green;">启用</span>';
        } else if (val == 0) {
            return '<span style="color:red;">禁用</span>';
        }
        return val;
    },
	typeFormat:function change(val) {
        if (val == 0) {
            return '普通用户';
        } else if (val == 1) {
            return '管理员';
        }
        return val;
    }
})